home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Newswatcher 2.0b22 / NW Source / Source / dialog.c < prev    next >
Encoding:
Text File  |  1994-12-05  |  65.0 KB  |  307 lines  |  [TEXT/MMCC]

  1. 5; c++) x->validChars[c] = isPrintable(c);
  2. }
  3.  
  4.  
  5.  
  6. /*----------------------------------------------------------------------------
  7.     MyGetNewDialog 
  8.     
  9.     Get a new dialog and intialize the dialog information.
  10.     
  11.     Entry:    id = resource id of DLOG resource.
  12.             defaultItem = item number of default button, or 0 if none.
  13.             cancelItem = item number of cancel button, or 0 if none.
  14.     
  15.     Exit:    function result = error code.
  16.             *theDialog = pointer to dialog record.
  17. ----------------------------------------------------------------------------*/
  18.  
  19. OSErr MyGetNewDialog (short id, short defaultItem, short cancelItem, 
  20.     DialogPtr *theDialog)
  21. {
  22.     DialogTemplate **template;
  23.     Boolean movableModal;
  24.     DialogPtr dlg = nil;
  25.     TWindow **info = nil;
  26.     TDialogItemInfo **itemInfo = nil;
  27.     TDialogItemInfo x;
  28.     short numItems, i;
  29.     Handle outlineDitl;
  30.     short itemType;
  31.     Handle itemHandle;
  32.     Rect itemRect, defaultRect;
  33.     OSErr err = noErr;
  34.     Boolean savedCriticalSeq;
  35.  
  36.     BeginCriticalMemorySequence(&savedCriticalSeq);
  37.     err = MyGetResource('DLOG', id, &template);
  38.     if (err != noErr) goto exit;
  39.     movableModal = (**template).procID == movableDBoxProc;
  40.     err = PrepUserInteraction();
  41.     if (err != noErr) goto exit;
  42.     dlg = GetNewDialog(id, nil, (WindowPtr)-1);
  43.     if (defaultItem != 0) {
  44.         err = MyGetResource('DITL', kDefaultButtonOutlineUserItemDitlID, &outlineDitl);
  45.         if (err != noErr) goto exit;
  46.         AppendDITL(dlg, outlineDitl, overlayDITL);
  47.         numItems = CountDITL(dlg);
  48.         GetDialogItem(dlg, defaultItem, &itemType, &itemHandle, &defaultRect);
  49.         GetDialogItem(dlg, numItems, &itemType, &itemHandle, &itemRect);
  50.         InsetRect(&defaultRect, -4, -4);
  51.         SetDialogItem(dlg, numItems, itemType, 
  52.             (Handle)gDlgDefaultButtonOutlineItemUPP, &defaultRect);
  53.     }
  54.     err = MyNewHandle(sizeof(TWindow), &info);
  55.     if (err != noErr) goto exit;
  56.     SetWRefCon(dlg, (long)info);
  57.     numItems = CountDITL(dlg);
  58.     err = MyNewHandle(numItems * sizeof(TDialogItemInfo), &itemInfo);
  59.     if (err != noErr) goto exit;
  60.     (**info).kind = kDialog;
  61.     (**info).movableModal = movableModal;
  62.     (**info).defaultItem = defaultItem;
  63.     (**info).defaultOutline = true;
  64.     (**info).cancelItem = cancelItem;
  65.     (**info).curItem = 0;
  66.     (**info).itemInfo = itemInfo;
  67.     InitDefaultItemInfo(&x);
  68.     for (i = 0; i < numItems; i++) (*itemInfo)[i] = x;
  69.     SetMenusTo(movableModal ? kAppleOnlyAboutDisabled : kAppleAllDisabled, 0, 0, 0, 0, 0);
  70.     HiliteMenu(0);
  71.     *theDialog = dlg;
  72.     gMyCurDialog = dlg;
  73.     EndCriticalMemorySequence(savedCriticalSeq);
  74.     return noErr;
  75.     
  76. exit:
  77.  
  78.     if (dlg != nil) DisposeDialog(dlg);
  79.     MyDisposeHandle(info);
  80.     MyDisposeHandle(itemInfo);
  81.     EndCriticalMemorySequence(savedCriticalSeq);
  82.     gMyCurDialog = nil;
  83.     return err;
  84. }
  85.  
  86.  
  87.  
  88. /*----------------------------------------------------------------------------
  89.     MyShortenDITL
  90.     
  91.     Shorten a dialog's item list.
  92.     
  93.     Entry:    dlg = pointer to dialog.
  94.             numberItems = number of items to remove from the dialog item list.
  95. ----------------------------------------------------------------------------*/
  96.  
  97. void MyShortenDITL (DialogPtr dlg, short numberItems)
  98. {
  99.     TWindow **info;
  100.     TDialogItemInfo **itemInfo;
  101.     TDialogItemInfo *pItemInfo;
  102.     short numItems, item;
  103.     char state;
  104.     
  105.     info = (TWindow**)GetWRefCon(dlg);
  106.     itemInfo = (**info).itemInfo;
  107.     numItems = CountDITL(dlg);
  108.     state = MyHGetState(itemInfo);
  109.     MyHLock(itemInfo);
  110.     for (item = numItems - numberItems + 1, pItemInfo = *itemInfo + item - 1; 
  111.         item <= numItems; 
  112.         item++, pItemInfo++) 
  113.     {
  114.         if (pItemInfo->theTE != nil) TEDispose(pItemInfo->theTE);
  115.         if (pItemInfo->vScroll != nil) DisposeControl(pItemInfo->vScroll);
  116.     }
  117.     MyHSetState(itemInfo, state);
  118.     ShortenDITL(dlg, numberItems);
  119.     numItems = CountDITL(dlg);
  120.     if ((**info).curItem > numItems) (**info).curItem = 0;
  121.     MySetHandleSize(itemInfo, numItems*sizeof(TDialogItemInfo));
  122. }
  123.  
  124.  
  125.  
  126. /*----------------------------------------------------------------------------
  127.     MyAppendDITL 
  128.     
  129.     Add items to a dialog's item list.
  130.     
  131.     Entry:    dlg = pointer to dialog.
  132.             theDITL = handle to item list to append.
  133.             theMethod = the manner in which the new items should be displayed,
  134.                 as in the Dialog Manger AppendDITL procedure.
  135.                 
  136.     Exit:    function result = error code.
  137. ----------------------------------------------------------------------------*/
  138.  
  139. OSErr MyAppendDITL (DialogPtr dlg, Handle theDITL, DITLMethod theMethod)
  140. {
  141.     TWindow **info;
  142.     TDialogItemInfo **itemInfo;
  143.     TDialogItemInfo x;
  144.     short oldNumItems, numItems, i;
  145.     OSErr err = noErr;
  146.  
  147.     oldNumItems = CountDITL(dlg);
  148.     AppendDITL(dlg, theDITL, theMethod);
  149.     info = (TWindow**)GetWRefCon(dlg);
  150.     itemInfo = (**info).itemInfo;
  151.     numItems = CountDITL(dlg);
  152.     err = MySetHandleSizeCritical(itemInfo, numItems*sizeof(TDialogItemInfo));
  153.     if (err != noErr) return err;
  154.     InitDefaultItemInfo(&x);
  155.     for (i = oldNumItems; i < numItems; i++) (*itemInfo)[i] = x;
  156.     return noErr;
  157. }
  158.  
  159.  
  160.  
  161. /*----------------------------------------------------------------------------
  162.     ScrollAction 
  163.     
  164.     Vertical scroll bar action proc.
  165.  
  166.     Entry:    vScroll = handle to vertical scroll bar control.
  167.             part = part code.
  168. ----------------------------------------------------------------------------*/
  169.  
  170. static pascal void ScrollAction (ControlHandle vScroll, short part)
  171. {
  172.     WindowPtr wind;
  173.     TWindow **info;
  174.     TDialogItemInfo **itemInfo;
  175.     short item;
  176.     TEHandle theTE;
  177.     
  178.     wind = (**vScroll).contrlOwner;
  179.     info = (TWindow**)GetWRefCon(wind);
  180.     itemInfo = (**info).itemInfo;
  181.     item = GetControlReference(vScroll);
  182.     theTE = (*itemInfo)[item-1].theTE;
  183.     SetControlReference(vScroll, 0);
  184.     TEScrollScrollByPartCode(theTE, vScroll, part);
  185.     SetControlReference(vScroll, item);
  186. }
  187.  
  188.  
  189.  
  190. /*----------------------------------------------------------------------------
  191.     DialogFilter 
  192.     
  193.     Universal modal dialog filter.
  194.         
  195.     Entry:    dlg = pointer to dialog.
  196.             ev = pointer to event record.
  197.             
  198.     Exit:    function result = true if event handled and item hit.
  199.             *itemHit = item number of item hit.
  200.             ev = pointer to possibly modified event record.
  201. ----------------------------------------------------------------------------*/
  202.  
  203. pascal Boolean DialogFilter (DialogPtr dlg, EventRecord *ev, short *itemHit)
  204. {
  205.     static RgnHandle cursorRgn = nil;
  206.     short itemType;
  207.     Handle itemHandle;
  208.     Rect box;
  209.     
  210.     if (ev->what != nullEvent) {
  211.         gPrevEvent = gCurEvent;
  212.         gCurEvent = *ev;
  213.     }
  214.     
  215.     if (cursorRgn == nil) cursorRgn = NewRgn();
  216.     HandleIdle(cursorRgn);
  217.  
  218.     gHandled = false;
  219.     gItem = 0;
  220.     gEv = ev;
  221.     
  222.     if (ev->what == updateEvt && (WindowPtr)ev->message == dlg) return false;
  223.     if (ev->what == updateEvt || ev->what == activateEvt) gHandled = true;
  224.     
  225.     HandleEvent(ev);
  226.     
  227.     if (gItem != 0) {
  228.         GetDialogItem(dlg, gItem, &itemType, &itemHandle, &box);
  229.         if ((itemType & itemDisable) != 0) {
  230.             ev->what = nullEvent;
  231.             return false;
  232.         } else {
  233.             *itemHit = gItem;
  234.             return true;
  235.         }
  236.     } else if (gHandled) {
  237.         ev->what = nullEvent;
  238.         return false;
  239.     } else {
  240.         return false;
  241.     }
  242. }
  243.  
  244.  
  245.  
  246. /*----------------------------------------------------------------------------
  247.     MyModalDialog 
  248.  
  249.     Present a modal dialog.
  250.     
  251.     Entry:    dlg = pointer to dialog window.
  252.             filterProc = filter proc UPP.
  253.             
  254.     Exit:    *itemHit = item number hit.
  255. ----------------------------------------------------------------------------*/
  256.  
  257. void MyModalDialog (DialogPtr dlg, ModalFilterUPP filterProc, short *itemHit)
  258. {
  259.     MyShowWindow(dlg);
  260.     ModalDialog(filterProc, itemHit);
  261. }
  262.  
  263.  
  264.  
  265. /*----------------------------------------------------------------------------
  266.     MyMovableModalDialog 
  267.  
  268.     Present a movable modal dialog.
  269.     
  270.     Entry:    dlg = pointer to dialog window.
  271.             filterProc = filter proc UPP.
  272.             
  273.     Exit:    *itemHit = item number hit.
  274. ----------------------------------------------------------------------------*/
  275.  
  276. void MyMovableModalDialog (DialogPtr dlg, ModalFilterProcPtr filterProc, short *itemHit)
  277. {
  278.     EventRecord ev;
  279.     Boolean gotEvt;
  280.     DialogPtr tempDlg;
  281.     GrafPtr port;
  282.     
  283.     GetPort(&port);
  284.     SetPort(dlg);
  285.  
  286.     MyShowWindow(dlg);
  287.  
  288.     while (true) {
  289.         gotEvt = WaitNextEvent(everyEvent & ~highLevelEventMask, &ev, 0, nil);
  290.         if (!gotEvt) ev.what = nullEvent;
  291.         if ((*filterProc)(dlg, &ev, itemHit)) goto exit;
  292.         if (!gHandled && IsDialogEvent(&ev) && 
  293.             DialogSelect(&ev, &tempDlg, itemHit)) goto exit;
  294.     }
  295.         
  296. exit:
  297.  
  298.     SetPort(port);
  299.     
  300. }
  301.  
  302.  
  303.  
  304. /*----------------------------------------------------------------------------
  305.     ErrorMessage 
  306.     
  307.     Issue an erro